package com.AAL.Action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.TypeOfAutoDAO;
import com.AAL.DAO.TypesOfWheelDAO;
import com.AAL.Model.TypeOfAuto;
import com.AAL.Model.TypesOfWheel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TypeOfWheelAction extends ActionSupport implements SessionAware {

	/*
	 * Khai báo các thuộc tính
	 */
	private TypesOfWheel typeOfWheel;
	private TypeOfAuto typeOfAuto;
	private List<TypesOfWheel> lstTypesOfWheels;
	private List<TypeOfAuto> lstTypeOfAutos;

	private TypeOfAutoDAO lnkTypeOfAutoDAO;
	private TypesOfWheelDAO lnkTypesOfWheelDAO;

	private Map<String, Object> session;

	public TypeOfWheelAction() {
		this.lnkTypeOfAutoDAO = new TypeOfAutoDAO();
		this.lnkTypesOfWheelDAO = new TypesOfWheelDAO();
	}

	/*
	 * ------------------------------------------------------- Minh
	 * -------------------------------------------------------
	 */

	public String typeOfWheelManagement() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			this.lstTypeOfAutos = this.lnkTypeOfAutoDAO.getAllTypeOfAutos();
			
			if (this.typeOfAuto!= null) {
				this.lstTypesOfWheels = this.lnkTypesOfWheelDAO
						.getLstTypesOfWheelsByTypeOfAutos(typeOfAuto);
			} else {
				
				if (this.lstTypeOfAutos.size() > 0) {
					this.lstTypesOfWheels = this.lnkTypesOfWheelDAO
							.getLstTypesOfWheelsByTypeOfAutos(this.lstTypeOfAutos
									.get(0));
				}
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String preSaveAndUpdateTypeOfWheel() {
		try {
			this.lstTypeOfAutos = this.lnkTypeOfAutoDAO.getAllTypeOfAutos();
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String saveAndUpdateTypeOfWheel() {
		try {
			if (typeOfWheel.getProductName().trim().length() == 0
					|| typeOfWheel.getTypeOfWheelID().trim().length() == 0) {
				addActionMessage("Ban chua nhap cac truong du lieu yeu cau.");
				this.lstTypeOfAutos = this.lnkTypeOfAutoDAO.getAllTypeOfAutos();
				return INPUT;
			} else {
				this.typeOfWheel = this.lnkTypesOfWheelDAO
						.saveAndUpdateTypesOfWheel(typeOfWheel);
				if (this.typeOfWheel == null) {
					addActionError("Không thể thêm mới dữ liệu.");
					return ERROR;
				}
			}

		} catch (Exception e) {
			addActionError("Error: " + e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	public String deleteTypeOfWheel() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			System.out.println("xoa mot type of wheel");
			System.out.println("ID:" + request.getParameter("id"));
			this.typeOfWheel = lnkTypesOfWheelDAO.deleteTypesOfWheel(request
					.getParameter("id"));
			if (this.typeOfWheel == null) {
				addActionError("Không thể xóa dữ liệu.");
				return ERROR;
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String getTypeOfWheelDetail() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			this.typeOfWheel = lnkTypesOfWheelDAO.getTypesOfWheel(request
					.getParameter("id"));
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String preEditSaveAndUpdateTypeOfWheel() {

		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {

			this.typeOfWheel = lnkTypesOfWheelDAO.getTypesOfWheel(request
					.getParameter("id"));
			this.lstTypeOfAutos = this.lnkTypeOfAutoDAO.getAllTypeOfAutos();
			// Cau lenh Sysout phia sau nham muc dich goi du lieu ra
			// >"<. ko co ko view dc >"<
			System.out.println("thong tin" + typeOfWheel.getProductName());

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * ------------------------------------------------------- ket thuc Minh
	 * -------------------------------------------------------
	 */

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public TypesOfWheel getTypeOfWheel() {
		return typeOfWheel;
	}

	public void setTypeOfWheel(TypesOfWheel typeOfWheel) {
		this.typeOfWheel = typeOfWheel;
	}

	public List<TypesOfWheel> getLstTypesOfWheels() {
		return lstTypesOfWheels;
	}

	public void setLstTypesOfWheels(List<TypesOfWheel> lstTypesOfWheels) {
		this.lstTypesOfWheels = lstTypesOfWheels;
	}

	public List<TypeOfAuto> getLstTypeOfAutos() {
		return lstTypeOfAutos;
	}

	public void setLstTypeOfAutos(List<TypeOfAuto> lstTypeOfAutos) {
		this.lstTypeOfAutos = lstTypeOfAutos;
	}

	public TypeOfAuto getTypeOfAuto() {
		return typeOfAuto;
	}

	public void setTypeOfAuto(TypeOfAuto typeOfAuto) {
		this.typeOfAuto = typeOfAuto;
	}

}
